<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<html>
<head>
	<title>user</title>
</head>
<body>
    Local: <br>
    <video id="localVideo" autoplay></video><br>
    Remote: <br>
    <video id="remoteVideo" autoplay></video>
	
	<script type="text/javascript">
	    // 与信令服务器的WebSocket连接
    	var socket = new WebSocket("wss://192.168.1.120:8443/RtcServer/rtc?type=user&uid=111&toId=222");
    	navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
   	    // stun和turn服务器
//         var configuration = { 'iceServers':[{'url':'stun:192.168.1.199'},{'url':'turn:mgt@192.168.1.199',credential:'mugutu'}]};
        window.RTCPeerConnection =  (window.PeerConnection || window.webkitPeerConnection00 || window.webkitRTCPeerConnection || window.mozRTCPeerConnection);
   	    
        //使用google的ice服务器
        var configuration = {
        	    "iceServers": [{
        	        "url": "stun:stun.l.google.com:19302"
        	    }]
        	};
	</script>
    <script>
		

        // 创建PeerConnection实例 (参数为null则没有iceserver，即使没有stunserver和turnserver，仍可在局域网下通讯)
        var pc = new RTCPeerConnection(configuration);

        // 发送ICE候选到其他客户端			(这里开始发送请求)
        pc.onicecandidate = function(event){
            if (event.candidate !== null) {
                socket.send(JSON.stringify({
                    "event": "_ice_candidate",
                    "data": {
                        "candidate": event.candidate
                    }
                }));
            }
        };

        // 如果检测到媒体流连接到本地，将其绑定到一个video标签上输出
        pc.onaddstream = function(event){
            document.getElementById('remoteVideo').src = URL.createObjectURL(event.stream);
        };

        // 发送offer和answer的函数，发送本地session描述
        var sendOfferFn = function(desc){
            pc.setLocalDescription(desc);
            if(socket!=null){
            	socket.send(JSON.stringify({ 
                    "event": "_offer",
                    "data": {
                        "sdp": desc
                    }
                }));
            }
        }
        
        var sendAnswerFn = function(desc){
            pc.setLocalDescription(desc);
            if(socket!=null){
           	  socket.send(JSON.stringify({ 
                     "event": "_answer",
                     "data": {
                         "sdp": desc
                     }
                }));
            }
        };

        // 获取本地音频和视频流
        navigator.getUserMedia({
            "audio": true,
            "video": true
        }, function(stream){
            //绑定本地媒体流到video标签用于输出
            document.getElementById('localVideo').src = URL.createObjectURL(stream);
            //向PeerConnection中加入需要发送的流
            pc.addStream(stream);
            //如果是发起方则发送一个offer信令
            pc.createOffer(sendOfferFn, function (error) {
                console.log('Failure callback: ' + error);
            });
        }, function(error){
            //处理媒体流创建失败错误
            console.log('getUserMedia error: ' + error);
        });

        //处理到来的信令
        socket.onmessage = function(event){
            var json = JSON.parse(event.data);
            console.log('onmessage: ', json);
            //如果是一个ICE的候选，则将其加入到PeerConnection中，否则设定对方的session描述为传递过来的描述
            if( json.event === "_ice_candidate" ){
                pc.addIceCandidate(new RTCIceCandidate(json.data.candidate));
            } else {
                pc.setRemoteDescription(new RTCSessionDescription(json.data.sdp));
                // 如果是一个offer，那么需要回复一个answer
                if(json.event === "_offer") {
                    pc.createAnswer(sendAnswerFn, function (error) {
                        console.log('Failure callback: ' + error);
                    });
                }
            }
        };
        
        socket.onclose = function(event){
        	console.debug("报警人关闭了websocket会话。。。");
        	socket == null;
        }
    </script>
</body>
</html>